//
//  CustomTableViewCell.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/23.
//

import UIKit

class CustomTableViewCell: UITableViewCell {
    // Title section
    private let titleContainer = UIView()
    private let titleLabel = UILabel()
    private let titleBackgroundImageView = UIImageView()
    private let titleIconImageView = UIImageView()
    private let titleApplyBgImageView = UIImageView()
    private let titleApplyLabel = UILabel()
    private let titleApplyArrowImageView = UIImageView()
    
    // Content section
    private let contentContainer = UIView()
    private let contentLabel = UILabel()
    private let contentBackgroundImageView = UIImageView()
    private let contentDetailLabel = UILabel()
    
    // Constants
    private let spacing: CGFloat = 8
    private let cornerRadius: CGFloat = 0
    private let padding: CGFloat = 12
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        // Configure title section
        titleContainer.clipsToBounds = true
        titleContainer.layer.cornerRadius = cornerRadius
        
        titleBackgroundImageView.contentMode = .scaleAspectFill
        titleBackgroundImageView.image = UIImage(named: "small_card_cell_top")
        titleBackgroundImageView.isUserInteractionEnabled = true
        titleContainer.addSubview(titleBackgroundImageView)
        titleContainer.addSubview(titleIconImageView)
        titleLabel.isUserInteractionEnabled = true
        titleContainer.addSubview(titleLabel)
        titleApplyBgImageView.isUserInteractionEnabled = true
        titleApplyBgImageView.image = UIImage(named: "small_card_cell_apply")
        titleContainer.addSubview(titleApplyBgImageView)
        
        titleApplyLabel.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        titleApplyLabel.text = LYlocalizedString("Apply")
        titleApplyLabel.textColor = UIColor(hex: "#FFFFFF")
        titleApplyBgImageView.addSubview(titleApplyLabel)
        titleApplyArrowImageView.image = UIImage(named: "small_card_cell_arrow")
        titleApplyBgImageView.addSubview(titleApplyArrowImageView)
        
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont.boldSystemFont(ofSize: 18)
        
        // Configure content section
        contentContainer.clipsToBounds = true
        contentContainer.layer.cornerRadius = cornerRadius
        
        backgroundColor = .clear
        contentBackgroundImageView.isUserInteractionEnabled = true
        contentBackgroundImageView.contentMode = .scaleAspectFill
        contentBackgroundImageView.image = UIImage(named: "small_card_cell_bottom")
        contentContainer.addSubview(contentBackgroundImageView)
        contentContainer.addSubview(contentLabel)
        contentDetailLabel.textColor = UIColor(hex: "#000000")
        contentDetailLabel.font = UIFont.systemFont(ofSize: 12)
        contentDetailLabel.textAlignment = .right
        contentContainer.addSubview(contentDetailLabel)
        
        contentLabel.numberOfLines = 0
        contentLabel.textColor = UIColor(hex: "#000000")
        contentLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        
        // Add to content view
        contentView.addSubview(titleContainer)
        contentView.addSubview(contentContainer)
    }
    
    private func setupConstraints() {
        // Title container constraints
        titleContainer.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(spacing)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        
        // Title background constraints
        titleBackgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleIconImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16 * xScale)
        }
        
        titleApplyBgImageView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }

        // Title label constraints
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(titleIconImageView.snp.trailing).offset(5)
        }
        
        titleApplyLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
        }
        
        titleApplyArrowImageView.snp.makeConstraints { make in
            make.leading.equalTo(titleApplyLabel.snp.trailing).offset(5)
            make.centerY.equalToSuperview()
        }
        
        // Content container constraints
        contentContainer.snp.makeConstraints { make in
            make.top.equalTo(titleContainer.snp.bottom)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-spacing)
        }
        
        // Content background constraints
        contentBackgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        // Content label constraints
        contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(padding)
        }
        
        contentDetailLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(padding + 10)
            make.centerY.equalToSuperview()
            
        }
    }
    
    func configure(with data: Receiveer) {
        
        if let baseURL = data.juxtaette {
            let queryString = NetworkManager.shared.commonParameters.toURLQueryString()
            let imageUrl = "\(baseURL)?\(queryString)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            titleIconImageView.sd_setImage(with: URL(string: imageUrl ?? ""), placeholderImage: nil, options: .retryFailed)
        }
        titleLabel.text = data.argue
        contentLabel.text = data.auctproof
        contentDetailLabel.text = (data.billionuous ?? "") + " " + "|" + " " + (data.vulnerness ?? "")
    }
}
